import App from './App'

// #ifndef VUE3
import Vue from 'vue'
import store from '@/store';
// main.js，注意要在use方法之后执行
import uView from 'uview-ui'
import api from "./request/api.js"


import $mAssetsPath from '@/config/assets.config.js';
import $mConfig from '@/config/index.config.js';
import $mRoutesConfig from '@/config/routes.config.js';
import $mFormRule from '@/config/formRule.config.js';
import $mConstDataConfig from '@/config/constData.config.js';
import $mSettingConfig from '@/config/setting.config.js';


import rfEmpty from '@/components/rf-empty';
Vue.component('rfEmpty', rfEmpty);

import $mHelper from '@/utils/helper';

import $mRouter from '@/utils/router';

Vue.use(uView)
// 如此配置即可
uni.$u.config.unit = 'rpx'
Vue.prototype.$http = api;
// 挂载全局自定义方法
Vue.prototype.$mStore = store;

Vue.prototype.$mRouter = $mRouter;

Vue.prototype.$mConfig = $mConfig;
Vue.prototype.$mAssetsPath = $mAssetsPath;
Vue.prototype.$mFormRule = $mFormRule;
Vue.prototype.$mHelper = $mHelper;
Vue.prototype.$mRoutesConfig = $mRoutesConfig;
Vue.prototype.$mConstDataConfig = $mConstDataConfig;
Vue.prototype.$mSettingConfig = $mSettingConfig;

Vue.config.productionTip = false

// 路由导航
$mRouter.beforeEach((navType, to) => {
	if (to.route === undefined) {
		throw '路由钩子函数中没有找到to对象，路由信息:' + JSON.stringify(to);
	}
	if (to.route === $mRoutesConfig.login.path && store.getters.hasLogin) {
		uni.reLaunch({
			url: $mHelper.objParseUrlAndParam($mRoutesConfig.main.path)
		});
		return;
	}
	// 过滤需要权限的页面
	if (to.route.requiresAuth) {
		if (store.getters.hasLogin) {
			// 已经登录
			uni[navType]({
				url: $mHelper.objParseUrlAndParam(to.route.path, to.query)
			});
		} else {
			// 登录成功后的重定向地址和参数
			const query = {
				redirectUrl: to.route.path,
				...to.query
			};
			// 没有登录 是否强制登录?
			if (store.state.forcedLogin) {
				uni.redirectTo({
					url: $mHelper.objParseUrlAndParam($mRoutesConfig.login.path, query)
				});
			} else {
				uni.navigateTo({
					url: $mHelper.objParseUrlAndParam($mRoutesConfig.login.path, query)
				});
			}
		}
	} else {
		uni[navType]({
			url: $mHelper.objParseUrlAndParam(to.route, to.query)
		});
	}
});
Vue.prototype.resetImgUrl = function(arr) {
    if (!arr) return arr
    let flg = Array.isArray(arr)
    let data = []
    arr = flg ? arr : [arr]

    let VUE_APP_BASE_API = "http://121.40.44.76:5000/" 

    arr.forEach(i => {
        if (i.indexOf("api") >= 0) {
            data.push(VUE_APP_BASE_API + i)
        } else {
            data.push(i)
        }
    });
    if (flg) {
        return data
    } else {
        return data[0]
    }
}

Vue.mixin({
	computed: {
		themeColor: {
			get () {
				return store.getters.themeColor;
			},
			set (val) {
				store.state.themeColor = val;
			}
		}
	}
});

App.mpType = 'app'
const app = new Vue({
    ...App
})
app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'

export function createApp() {
  const app = createSSRApp(App)
  return {
    app
  }
}
// #endif